package com.flower.controller;


import com.flower.pojo.Store;
import com.flower.pojo.User;
import com.flower.service.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;


    //    根据用户名查询
    @RequestMapping("/query_user_all")
    @ResponseBody
    public Map<String, Object> queryUser(String uname, Integer currpage, Integer state) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("uname", uname);
        map1.put("state", state);
        PageHelper.startPage(currpage, 5);
        map.clear();
        List<User> list = adminService.queryUserAll(map1);
        PageInfo info = new PageInfo(list);
        if (!list.isEmpty() || list != null) {
            map.put("code", "200");
            map.put("msg", "查询用户成功");
            map.put("data", list);
            map.put("info", info);
        } else {
            map.put("code", "201");
            map.put("msg", "没有用户相关信息");
        }
        return map;
    }


    //    根据商家名查询
    @RequestMapping("/query_store_all")
    public Map<String, Object> queryStore(String sname, Integer currpage, Integer state) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map1 = new HashMap<>();
        PageHelper.startPage(currpage, 5);
        map1.put("sname", sname);
        map1.put("state", state);

        List<Store> list = adminService.queryStoreAll(map1);
        PageInfo<Store> info = new PageInfo(list);
        if (!list.isEmpty() || list != null) {
            map.put("code", "200");
            map.put("msg", "查询商家成功");
            map.put("data", list);
            map.put("info", info);
        } else {
            map.put("code", "201");
            map.put("msg", "没有商家相关信息");
        }
        return map;
    }

    //    拉黑用户
    @RequestMapping("/update_user_state")
    public Map<String, Object> updateUserState(User user, int uid, Integer state) {
        Map<String, Object> map = new HashMap<>();
        if (uid != 0) {
            user.setUid(uid);
            //拉黑
            user.setState(state);
            int result = adminService.updateUserState(user);
            if (result == 1) {
                map.put("code", "200");
                map.put("msg", "拉黑用户成功");

            } else {
                map.put("code", 201);
                map.put("msg", "拉黑用户失败，请联系管理员18742590833");
            }
        } else {
            System.out.println("用户ID空了");

        }
        return map;

    }


    //    拉黑商家
    @RequestMapping("/update_store_state")
    public Map<String, Object> updateStoreState(Store store, HttpSession session, int sid, Integer state) {
        Map<String, Object> map = new HashMap<>();
//        int sid = ((Store) session.getAttribute("store")).getSid();
        if (sid != 0) {
            store.setSid(sid);
            //拉黑
            store.setState(state);
            int result = adminService.updateStoreState(store);
            if (result == 1) {
                map.put("code", "200");
                map.put("msg", "拉黑商家成功");

            } else {
                map.put("code", 201);
                map.put("msg", "拉黑商家失败，请联系管理员18742590833");
            }
        } else {
            System.out.println("商家ID空了");

        }
        return map;

    }


    @RequestMapping("/queryAllStore1")
    public Map<String, Object> queryStoreAll1() {
        Map<String, Object> resultMap = new HashMap<>();
        List<Store> stores = adminService.queryStoreAll1();
        resultMap.put("data", adminService.queryStoreAll1());
        return resultMap;
    }

}
